package com.imddy.webmon.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor {
	
	private static String LoginSessionKey = "LoginCheckUser";

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("LoginInterceptor afterCompletion");
		
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView mav)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("LoginInterceptor postHandle");
		
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("LoginInterceptor preHandle ");
		String loginUrl = request.getRequestURI();
		String context = request.getContextPath();
		System.out.println("context: " + context);
		System.out.println("loginUrl: " + loginUrl);
		String testUrl = context + "/login.do";
		if ( testUrl.equals(loginUrl)) {
			System.out.println("==");
			return true;
		}
		HttpSession session = request.getSession();
		Object sessionKey = session.getAttribute(LoginSessionKey);
		if (null != sessionKey) {
			System.out.println("已登陆");
			return true;
		}
		response.sendRedirect("login.do");
		return false;
	}

}
